//
//  HZUIViewForGuide.m
//  Constraint
//
//  Created by huazi on 14-5-28.
//  Copyright (c) 2014年 AutoLayoutTestDemo. All rights reserved.
//

#import "HZUIViewForGuide.h"
#define DeviceRect1   [UIScreen mainScreen].bounds
#define DeviceHeight1 [UIScreen mainScreen].bounds.size.height
#define DeviceWidth1 [UIScreen mainScreen].bounds.size.width
#define FirstUsedApp [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] substringToIndex:1]
@interface HZUIViewForGuide ()<UIScrollViewDelegate>
{
    NSArray *arrayPic;
    int countsPic;
}
@end

@implementation HZUIViewForGuide

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

- (void)initSubviews:(UIView *)view
{
    self.scrollView =[[UIScrollView alloc] initWithFrame:self.frame];
    [self addSubview:self.scrollView];
    if (self.dataSource!=nil &&[self.dataSource respondsToSelector:@selector(ViewForGuideArrayPics:)])
    {
        
        arrayPic =[self.dataSource ViewForGuideArrayPics:self];
        countsPic =[arrayPic count];
        self.scrollView.contentSize =CGSizeMake(DeviceWidth1*countsPic, DeviceHeight1);
        self.scrollView.pagingEnabled =YES;
        self.scrollView.bounces =NO;
        self.scrollView.delegate = self;
        self.scrollView.showsVerticalScrollIndicator =NO;
        self.scrollView.showsHorizontalScrollIndicator =NO;
        for (int i=0; i<countsPic; i++)
        {
            UIImageView *image =[[UIImageView alloc] initWithFrame:CGRectMake(0+i*DeviceWidth1, 0, DeviceWidth1, DeviceHeight1)];
            image.image =[UIImage imageNamed:[arrayPic objectAtIndex:i]];
            [self.scrollView addSubview:image];
            if (i==countsPic-1)
            {
                UITapGestureRecognizer *sinletap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapToHideSelf:)];
                sinletap.numberOfTapsRequired =1;
                image.userInteractionEnabled =YES;
                [image addGestureRecognizer:sinletap];
                
            }
            
        }
    }
    
    
    [view addSubview:self];
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, DeviceHeight1-50, 320, 48)];
    [_pageControl setNumberOfPages:4];
    [_pageControl setCurrentPage:0];
    [self addSubview:_pageControl];
   
   
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1
{
    int index = scrollView1.contentOffset.x/320;
    [_pageControl setCurrentPage:index];
}
- (void)TapToHideSelf:(id)sender
{
    [UIView animateWithDuration:0.5
                     animations:^{
                         
                         self.alpha =0.0f;
                     }
                     completion:^(BOOL finish)
     {
         [self removeFromSuperview];
         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:FirstUsedApp];
         
     }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
